package com.woniuxy.microsystemsarserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microsystemsarserver.bean.PageRequestBean;
import com.woniuxy.microsystemsarserver.bean.TerminalBean;
import com.woniuxy.microsystemsarserver.mapper.TerminalMapper;
import com.woniuxy.microsystemsarserver.service.ITerminalService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 终端管理业务层实现类
 * @author 杨灿
 */
@Transactional(readOnly = true)
@Service
public class TerminalServiceImpl implements ITerminalService {
    @Resource
    private TerminalMapper terminalMapper;

    @Override
    public PageInfo<TerminalBean> findByPage(TerminalBean terminalBean, PageRequestBean pageRequestBean,Long userId) {
        return PageHelper.startPage(pageRequestBean.getPageNum(),pageRequestBean.getLimit()).
                doSelectPageInfo(()->terminalMapper.findByPage(terminalBean,pageRequestBean,userId));
    }

    @Override
    public List<TerminalBean> findAll(Long userId) {

        return terminalMapper.findAll(userId);
    }

    @Override
    public TerminalBean findById(Long terminalId) {

        return terminalMapper.findById(terminalId);
    }
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Override
    public int updateTerminal(TerminalBean terminalBean) {

        return terminalMapper.updateTerminal(terminalBean);
    }
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Override
    public int saveTerminal(TerminalBean terminalBean) {
        long time = new Date().getTime();
        terminalBean.setTerminalNum(time+"");
        return terminalMapper.saveTerminal(terminalBean);
    }
}
